﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;

public partial class ProjectManage_SingleProject_Add : System.Web.UI.Page
{
    SingleProjectBLL bll = new SingleProjectBLL();
    TotalProjectBLL tbll = new TotalProjectBLL();
    private string sid = String.Empty;
    private string tid = String.Empty;
    private string pid = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            tid = this.Request.QueryString["t"].ToString().Trim();
            pid = this.Request.QueryString["p"].ToString().Trim();

        }
        catch
        {
        }
        try
        {
            sid = this.Request.QueryString["id"].ToString().Trim();
        }
        catch
        { }
        if (!IsPostBack)
        {
            TotalProjectEntity tp = tbll.GetModelEntity(int.Parse(pid));
            labprojectname.Text = tp.name;
            labprojectno.Text = tp.projectno;
            lblstage.Text = tp.stage;
            lblconstruction.Text = String.Empty;
            lblprojecttype.Text = tp.projecttype;
            lblprogresstype.Text = tp.progresstype;
            lblfileno.Text = String.Empty;
            lblfinishdate.Text = tp.tpdate.ToString();
            lblcommpany.Text = tp.commpany;
            labtpdate.Text = tp.tpdate.ToString();
            labcontactname.Text = String.Empty;
            labcontacttel.Text = String.Empty;
            labcontactdept.Text = String.Empty;
            labperson1.Text = tp.personname1;
            labperson2.Text = tp.personname2;
            if (tid == "1")
            {
                SingleProjectEntity sp = bll.GetModelEntity(int.Parse(sid));
                txtspno.Text = sp.spno;
                ddllistno.SelectedValue = sp.listno;
                ddlvolumeno.SelectedValue = sp.volumeno;
                txtspname.Text = sp.spname;
                txtfix.Text = sp.fix;

                ChoiceName_1.Text = sp.personname;
                HChoiceID_1.Value = sp.person.ToString();
                HNewChoiceID_1.Value = sp.person.ToString();

                ChoiceName_2.Text = sp.designername;
                HChoiceID_2.Value = sp.designer.ToString();
                HNewChoiceID_2.Value = sp.designer.ToString();

                ChoiceName_8.Text = sp.proofreadername;
                HChoiceID_8.Value = sp.proofreader.ToString();
                HNewChoiceID_8.Value = sp.proofreader.ToString();

                ChoiceName_4.Text = sp.chargername;
                HChoiceID_4.Value = sp.charger.ToString();
                HNewChoiceID_4.Value = sp.charger.ToString();

                ChoiceName_5.Text = sp.headname;
                HChoiceID_5.Value = sp.head.ToString();
                HNewChoiceID_5.Value = sp.head.ToString();

                ChoiceName_6.Text = sp.branchreviewername;
                HChoiceID_6.Value = sp.branchreviewer.ToString();
                HNewChoiceID_6.Value = sp.branchreviewer.ToString();

                ChoiceName_7.Text = sp.Standardizationname;
                HChoiceID_7.Value = sp.Standardization.ToString();
                HNewChoiceID_7.Value = sp.Standardization.ToString();

                ChoiceName_3.Text = sp.reviewername;
                HChoiceID_3.Value = sp.reviewer.ToString();
                HNewChoiceID_3.Value = sp.reviewer.ToString();

                string[] ary = sp.design.Split(',');
                for (int i = 0; i < ary.Length; i++)
                {
                    CheckBox cb = (CheckBox)this.FindControl("CheckBox" + ary[i].Trim());
                    if (cb != null)
                        cb.Checked = true;
                }

            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        SingleProject sp = new SingleProject();
        sp.spno = txtspno.Text;
        sp.spname = txtspname.Text;
        sp.listno = ddllistno.SelectedValue;
        sp.volumeno = ddlvolumeno.SelectedValue;
        sp.fix = txtfix.Text;
        string chk = String.Empty;
        for (int i = 1; i <= 25; i++)
        {
            CheckBox cb = (CheckBox)this.FindControl("CheckBox" + i);
            if (cb.Checked)
            {
                chk += String.IsNullOrEmpty(chk) ? i.ToString() : "," + i.ToString();
            }
        }
        sp.design = chk;
        if (HNewChoiceID_1.Value == "")
        {
            sp.person = 0;
        }
        else
        {
            sp.person = int.Parse(HNewChoiceID_1.Value);
        }
        if (HNewChoiceID_2.Value == "")
        {
            sp.designer = 0;
        }
        else
        {
            sp.designer = int.Parse(HNewChoiceID_2.Value);
        }
        if (HNewChoiceID_8.Value == "")
        {
            sp.proofreader = 0;
        }
        else
        {
            sp.proofreader = int.Parse(HNewChoiceID_8.Value);
        }
        if (HNewChoiceID_3.Value == "")
        {
            sp.reviewer = 0;
        }
        else
        {
            sp.reviewer = int.Parse(HNewChoiceID_3.Value);
        }
        if (HNewChoiceID_4.Value == "")
        {
            sp.charger = 0;
        }
        else
        {
            sp.charger = int.Parse(HNewChoiceID_4.Value);
        }
        if (HNewChoiceID_5.Value == "")
        {
            sp.head = 0;
        }
        else
        {
            sp.head = int.Parse(HNewChoiceID_5.Value);
        }
        if (HNewChoiceID_6.Value == "")
        {
            sp.branchreviewer = 0;
        }
        else
        {
            sp.branchreviewer = int.Parse(HNewChoiceID_6.Value);
        }
        if (HNewChoiceID_7.Value == "")
        {
            sp.Standardization = 0;
        }
        else
        {
            sp.Standardization = int.Parse(HNewChoiceID_7.Value);
        }

        if (txtprojectcost.Text == "")
        {
            sp.Investment = 0;
        }
        else
        {
            sp.Investment = Decimal.Parse(txtInvestment.Text);
        }
        if (txtprojectcost.Text == "")
        {
            sp.projectcost = 0;
        }
        else
        {
            sp.projectcost = Decimal.Parse(txtprojectcost.Text);
        }
        if (txtdesignfees.Text == "")
        {
            sp.designfees = 0;
        }
        else
        {
            sp.designfees = Decimal.Parse(txtdesignfees.Text);
        }
        sp.accessories = txtaccessories.Text;
        sp.cooperation = String.Empty;
        sp.surveydate = DateTime.Now;
        sp.tp_id = int.Parse(pid);
        if (tid == "0")
        {
            if(bll.Add(sp)==1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功！');</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败！');</script>");
            }
        }
        else
        {
            sp.sp_id = int.Parse(sid);
            if(bll.Update(sp)==1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功！');</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败！');</script>");
            }
        }
    }
}
